
package com.be.beadmin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.util.StringUtils;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.service.dto.input.PrintDesignItemDto;
import com.be.beadmin.service.dto.input.PrintDesignItemQueryCriteria;
import com.be.beadmin.service.dto.output.PrintDesignFileDetailEx;
import com.be.beadmin.service.dto.output.PrintDesignItemEx;
import com.be.beadmin.service.dto.output.PrintDesignItemList;
import com.be.beadmin.service.entity.PrintDesignFile;
import com.be.beadmin.service.entity.PrintDesignFile_;
import com.be.beadmin.service.entity.PrintDesignItem;
import com.be.beadmin.service.entity.PrintDesignItem_;
import com.be.beadmin.service.mapper.PrintDesignItemMapper;
import com.be.beadmin.service.mapper.PrintDesignItemMapperEx;
import com.be.beadmin.service.repository.PrintDesignFileRepository;
import com.be.beadmin.service.repository.PrintDesignItemRepository;
import com.be.beadmin.service.service.PrintDesignItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @description 服务实现
* @author chenwei
* @date 2023-05-10
**/
@Service
@RequiredArgsConstructor
public class PrintDesignItemServiceImpl implements PrintDesignItemService {

    @Autowired
    private  PrintDesignItemRepository printDesignItemRepository;
    @Autowired
    private  PrintDesignItemMapper printDesignItemMapper;
    @Autowired
    private  PrintDesignItemMapperEx printDesignItemExMapper;


    @Autowired
    private PrintDesignFileRepository printDesignFileRepository;

    @Override
    public Page<PrintDesignItemEx> getPrintDesignItemByPage(PrintDesignItemQueryCriteria pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<PrintDesignItem> page = printDesignItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(PrintDesignItem_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    if (!StringUtils.isEmpty(pageEntity.getPrintDesignFileId())) {
                        Predicate pre3 = criteriaBuilder.equal(root.get(PrintDesignItem_.printDesignFile)
                                .get(PrintDesignFile_.id), pageEntity.getPrintDesignFileId()); //过滤为0
                        list.add(pre3);
                    }
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(PrintDesignItem_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<PrintDesignItemEx> page1 = page.map(printDesignItemExMapper::toEntity);
            return page1;

    }

    @Override
    public void addPrintDesignItem(PrintDesignItemDto printDesignItemDto) {

        PrintDesignFile printDesignFile = printDesignFileRepository.findPrintDesignFileByIdAndDelete(printDesignItemDto.getPrintDesignFileId(), 0);
        if(ObjectUtil.isEmpty(printDesignFile)) {
            throw new BeadminException("请先选择需要设计的模板");
        }
        PrintDesignItem printDesignItem = printDesignItemMapper.toEntity(printDesignItemDto);
        printDesignItem.setPrintDesignFile(printDesignFile);
        printDesignItemRepository.save(printDesignItem);

    }

    @Override
    public void updatePrintDesignItem(PrintDesignItemDto printDesignItemDto) {
            PrintDesignItem printDesignItem = printDesignItemRepository.findPrintDesignItemById(printDesignItemDto.getId());
            printDesignItem = printDesignItemMapper.toEntity(printDesignItemDto);
            printDesignItemRepository.save(printDesignItem);
    }


    @Override
    public void delPrintDesignItem(String id) {
        PrintDesignItem printDesignItem = printDesignItemRepository.findPrintDesignItemById(id);
        printDesignItem.setDelete(1);
        printDesignItemRepository.save(printDesignItem);
    }

    @Override
    public void delPrintDesignItems (List<String> ids) { }

    @Override
    public PrintDesignFileDetailEx getDetailById(String id) {
        PrintDesignFileDetailEx printDesignFileDetailEx = new PrintDesignFileDetailEx();
        PrintDesignFile printDesignFile = printDesignFileRepository.findPrintDesignFileByIdAndDelete(id, 0);
        if(ObjectUtil.isEmpty(printDesignFile)) {
            throw new BeadminException("请先选择需要设计的模板");
        }
        List<PrintDesignItem> printDesignItems = printDesignItemRepository.findPrintDesignItemsByPrintDesignFile(printDesignFile);
        printDesignFileDetailEx.setId(printDesignFile.getId());
        printDesignFileDetailEx.setCode(printDesignFile.getCode());
        printDesignFileDetailEx.setName(printDesignFile.getName());
        printDesignFileDetailEx.setFileContext(printDesignFile.getFileContext());

        List<PrintDesignItemList> printDesignItemLists = printDesignItems.stream().map(item -> {
            PrintDesignItemList printDesignItemList = new PrintDesignItemList();
            printDesignItemList.setField(item.getField());
            printDesignItemList.setText(item.getText());
            return printDesignItemList;
        }).collect(Collectors.toList());
        printDesignFileDetailEx.setItems(printDesignItemLists);

        return printDesignFileDetailEx;
    }
}